package jtk.project4.fleet.task;

import java.util.List;

import nl.coderight.jazz.Task;
import nl.coderight.jazz.event.MessageEvent;
import nl.coderight.jazz.event.ProgressEvent;
import nl.coderight.jazz.event.MessageEvent.MessageEventType;
import nl.coderight.jazz.event.ProgressEvent.ProgressEventType;

import jtk.project4.fleet.domain.EmployeeC;
import jtk.project4.fleet.event.EmployeeEvent;
import jtk.project4.fleet.event.EmployeeEvent.EmployeeEventType;
import jtk.project4.fleet.service.DatabaseService;


public class LoadEmployeeCTask extends Task<List<EmployeeC>>
{
	
	@Override
	public List<EmployeeC> process() throws Exception
	{
		return DatabaseService.getInstance().getEmployeecs();
	}
	
	@Override
	protected void onStart() 
	{
		ProgressEvent progressEvent = new ProgressEvent(ProgressEventType.START);
		progressEvent.setMessage("Loading employee...");
		progressEvent.setIndeterminate(true);
		postEvent(progressEvent);
	}
	
	@Override
	protected void onFinish() {
		postEvent(new ProgressEvent(ProgressEventType.STOP));
		
		EmployeeEvent employeesEvent = new EmployeeEvent(EmployeeEventType.LOAD_SUCCESS);
		employeesEvent.setEmployeecs(getResult());
		postEvent(employeesEvent);
	}
	
	@Override
	protected void onError() 
	{
		MessageEvent error = new MessageEvent(MessageEventType.ERROR);
		error.setMessage(getError().getMessage());
		postEvent(error);			
	}

}
